$(function() {
	$('#serviceSel').mySelected({
		btnWidth: '100%',
		btnStyle: 'secondary'
	});
	$("form").validate({
		submitHandler : function(form) {
			var length = $("#form").find('input:checkbox[class=needCkCls]:checked').length;
			if(length==0){
				layer.alert("请选择服务和定价");
			}else{
				form.submit();
			}
		},
		rules : {
			"name" : {
				required : true,
				remote : { 
					type : "POST",
					url : 'management/package/isExist',
					data : {
						name : function() {
							return $("#name").val();
						},
						packageId : function() {
							return $("#id").val();
						}
					}
				}
			}
		},
		messages : {
			"name" : {
				required : "请输入套餐名称",
				remote :"该套餐名称已存在"
			}
		}
	});
	
	//check事件
	$(".needCkCls").click(function() {
		checkShopGrade(this);
    });
	
	/**
	 * 如果是详情页面，禁用所有表单元素，只能查看
	 */
	if(window.location.href.indexOf("detail")>0){
		$(":input").attr("disabled","true");
	}
});

/**
 * checkbox点击事件
 * @param obj
 */
function checkShopGrade(obj){
    if(obj.checked){
    	$(obj).siblings(":input").prop("disabled",false);
    	$(obj).parent().siblings().find(":input").prop("disabled",false);
    }else{
    	$(obj).siblings(":input").prop("disabled",true);
    	$(obj).parent().siblings().find(":input").prop("disabled",true);
    }
}

/**
 * 选择服务时创建div
 * @param index
 * @param id
 * @param name
 * @param shopId
 * @param shopName
 * @returns {String}
 
function createDiv(index,id,name,shopId,shopName){
	var html = "<div class='am-g am-margin-top' style='height:35px'>";
    html+= "<div class='am-u-sm-4 am-u-md-2 am-text-right'>"+name;
   	html+= "<input name='priceList["+index+"].serviceItemId' type='hidden' value='"+id+"' disabled='disabled'></div>";
	html+= "<div class='am-u-sm-4 am-u-md-2'>";
	html+= "<input type='checkbox' class='needCkCls' title='请选择门店等级' onClick='checkShopGrade(this)'> "+shopName;
	html+= "<input name='priceList["+index+"].shopGradeId' type='hidden' value='"+shopId+"' disabled='disabled'></div>";
	html+= "<div class='am-u-sm-4 am-u-md-2'>";
	html+= "<input type='text' name='priceList["+index+"].vipPrice' class='am-input-sm' placeholder='请输入会员价' disabled='disabled'></div>";
	html+= "<div class='am-u-md-2 am-u-end'>";
	html+= "<input type='text' name='priceList["+index+"].noVipPrice' class='am-input-sm' placeholder='请输入非会员价' disabled='disabled'></div>";
    html+= "</div>";
    return html;
}*/